/**
 * @Title: UserService.java
 * @Description:
 * @author zsw
 * <p>
 * Copyright(C) 2018-2019    NPlus
 * Company:   无锡核心信息科技有限公司
 * @version V2.0.0
 * @Date: 13:16 2020/4/2
 * <p>
 * 修改历史:
 * Date                 Author        Version        Description
 * -----------------------------------------------------------------------------------
 * 13:16 2020/4/2       zsw        2.0.0           初始化版本
 */
package com.example.dataTransactionService;

import com.example.dataTransaction.TransactionManager;
import com.example.dataTransactionDao.UserAccountDao;
import com.example.dataTransactionDao.UserOrderDao;

import javax.sql.DataSource;
import java.sql.SQLException;

public class UserService {
    private UserAccountDao userAccountDao;
    private UserOrderDao userOrderDao;
    private TransactionManager transactionManager;

    public UserService(DataSource dataSource) {
        userAccountDao = new UserAccountDao(dataSource);
        userOrderDao = new UserOrderDao(dataSource);
        transactionManager = new TransactionManager(dataSource);
    }

    public void action() {
        try {
            transactionManager.start();
            userOrderDao.order();
            userAccountDao.buy();
            transactionManager.close();
        } catch (SQLException e) {
            e.printStackTrace();
            transactionManager.rollback();
        }
    }
}
